package com.uk.ui.fatura;

import java.util.Collection;
import java.util.List;

import com.uk.data.containers.TarifaContainer;
import com.uk.data.ejbs.IFaturaBean;
import com.uk.data.entities.Fatura;
import com.uk.data.entities.Kontrata;
import com.uk.data.entities.StatusEnum;
import com.uk.interfaces.PopupWindowListener;
import com.uk.ui.CHorizontalSplitPanel;
import com.uk.ui.tarifa.TarifaNavigationTree;
import com.uk.ui.windows.Fatura2SelectWindow;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Panel;
import com.vaadin.ui.SplitPanel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class FaturaPanel extends CHorizontalSplitPanel implements ItemClickListener, ValueChangeListener, PopupWindowListener {

	private FaturaForm faturaForm;
	
	private FaturaNavigationTree faturaNavigationTree = new FaturaNavigationTree(this);
	
	private Fatura fatura;
	private IFaturaBean faturaBean;
	private Fatura2SelectWindow fatura2SelectWindow;
	private FaturaSearchView faturaSearchView;
	private FaturaView faturaView;
	
	public FaturaPanel(IFaturaBean faturaBean){

		this.faturaBean = faturaBean;
		faturaForm = new FaturaForm(this);
		this.setSizeFull();
		this.setSplitPosition(150, SplitPanel.UNITS_PIXELS);
		this.setFirstComponent(this.faturaNavigationTree);
		this.setSecondComponent(this.faturaForm);
		
		
		
	}

	private Object selectedMenu;
	
	
	@Override
	public void itemClick(ItemClickEvent event) {
		if (event.getSource() == this.faturaNavigationTree){
			Object itemId = event.getItemId();
			if (itemId != null){
				this.selectedMenu = itemId;
				
				if (FaturaNavigationTree.KRIJO.equals(itemId)){
					if (this.faturaForm == null) this.faturaForm= new FaturaForm(this);
					else this.faturaForm.resetForm();
					setSecondComponent(this.faturaForm);
				}
				else {
					if (this.faturaSearchView == null) this.faturaSearchView= new FaturaSearchView(this);
					if (FaturaNavigationTree.MBYLL.equals(itemId)) this.faturaSearchView.setCaption("Mbyll fature");
					else if (FaturaNavigationTree.PRINTO.equals(itemId)) this.faturaSearchView.setCaption("Printo fature");
					else if (FaturaNavigationTree.ANULLO.equals(itemId)) this.faturaSearchView.setCaption("Anullo fature");
					setSecondComponent(this.faturaSearchView);
				}
			
				
			}
		}
		
	}

	@Override
	public void valueChange(ValueChangeEvent event) {
		// TODO Auto-generated method stub
		
	}


	public IFaturaBean getFaturaBean() {
		return faturaBean;
	}


	public void setFaturaBean(IFaturaBean faturaBean) {
		this.faturaBean = faturaBean;
	}                            


	public void searchKontrata(Integer id) {
		Kontrata kontrata = this.faturaBean.queryKontrata(id);
		if (kontrata == null){
			Window.Notification n = new Window.Notification("Info",
	                Window.Notification.TYPE_TRAY_NOTIFICATION);
	        n.setPosition(Window.Notification.POSITION_CENTERED);
	        n.setDescription("Nuk ka asnje kontrate me ID e dhene!");
	        getWindow().showNotification(n);
			return;
		}
		if (FaturaNavigationTree.MBYLL.equals(this.selectedMenu))
			this.displayFatura(kontrata, false);
		else
			this.displayFatura(kontrata, null);
	
	}

	private void displayFatura(Kontrata kontrata, Boolean paguar){
		List<Fatura> faturaList = faturaBean.queryFatura(kontrata, paguar);
		if (faturaList == null || faturaList.isEmpty()){
			Window.Notification n = new Window.Notification("Info",
	                Window.Notification.TYPE_TRAY_NOTIFICATION);
	        n.setPosition(Window.Notification.POSITION_CENTERED);
	        String result = "Kontrata e zgjedhur: <br/>ID:<strong>"  + kontrata.getId() + "</strong>" +
			"<br/>Emri:<strong>"  + kontrata.getEmri() + "</strong>" +
			"<br/>Mbiemri:<strong>"  + kontrata.getMbiemri() + "</strong>" +
			"<br/>Adresa:<strong>"  + kontrata.getAdresa() + "</strong>" +
			"<br/>Perdorimi:<strong>"  + kontrata.getPerdorimi().getTag() +"</strong>" ;
	        
	        n.setDescription(result + "<br/><br/>nuk ka asnje fature !");
	        getWindow().showNotification(n);
			return;
		}
		this.fatura2SelectWindow = new Fatura2SelectWindow(this, kontrata, faturaList);
		getApplication().getMainWindow().addWindow(fatura2SelectWindow);
	}
	

	@Override
	public void returnAction(Object object) {
		
		
		
		this.faturaView = new FaturaView(fatura2SelectWindow.getSelectedFatura(), faturaBean, selectedMenu);
		this.getApplication().getMainWindow().removeWindow(fatura2SelectWindow);
		fatura2SelectWindow = null;
		
		this.setSecondComponent(this.faturaView);
		
		
	}


	public Object getSelectedMenu() {
		return this.selectedMenu;
	}


	public void setSelectedMenu(Object selectedMenu) {
		this.selectedMenu = selectedMenu;
		
	}

	

}
